/**
 * 
 */
package com.cipe.cmrs.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cipe.cmrs.model.Keyword;
import com.cipe.cmrs.service.KeywordService;

/**
 * @author Incepio
 *
 */
@Controller
public class KeywordController extends BaseController {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -3823562098119817733L;
	@Autowired
	private KeywordService		keywordService;

	@RequestMapping("/secure/saveKeyword.do")
	public String SaveKeyword(@ModelAttribute("keyword") Keyword keyword, BindingResult result, Model model, HttpServletRequest request) {
		try {
			keyword = keywordService.saveKeyword(keyword, getUserId(request));
			saveMessage(request, getText("editKeyword.message.success"));
		} catch (Exception e) {
			logger.error(getText("error.transaction"), e);
			saveError(request, getText("error.transaction"));
		}
		model.addAttribute("keyword", keyword);
		return "editKeyword";
	}

	@RequestMapping("/secure/showAddKeyword.do")
	public String showAddKeyword(Model model) {
		model.addAttribute("keyword", new Keyword());
		return "editKeyword";
	}

	@RequestMapping("/secure/showEditKeyword.do")
	public String showEditKeyword(@RequestParam Integer id, Model model) {
		model.addAttribute("keyword", keywordService.findById(id));
		return "editKeyword";
	}

	@RequestMapping("/secure/showKeywordList.do")
	public String showKeywordList(Model model) {
		model.addAttribute("keywords", keywordService.findAll());
		return "keywordList";
	}
}
